package com.ubing.boot.notesforjava.test.lombok;

import lombok.*;
import lombok.experimental.Accessors;
import lombok.experimental.Tolerate;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class LombokDemo {
    @Data
//    @Value  //有参构造
    private static class Demo1{
        private String name;
        private Integer age;
        private Byte sex;
    }
    @Builder
    @ToString
    private static class Demo2{
        @Tolerate
        public Demo2(){}

        private String name;
        private Integer age;
        private Byte sex;
        @Singular("addHobby")
        private List<String> hobby;

        public static void main(String[] args) throws InstantiationException, IllegalAccessException {
            Demo2 demo21 = Demo2.class.newInstance();// 调用的五参构造，@Builder会吧无参构造整没有  使用 @Tolerate 使lombok不接管该注解下的方法，属性等
            Demo1 demo11 = Demo1.class.newInstance();
            System.out.println(demo11); // @Data 不会出现上述异常
            Demo1 demo1 = new Demo1();
            Demo2 demo2 = Demo2.builder().name("123").age(12).addHobby("1233").addHobby("69666").build();
            System.out.println(demo21);
        }
    }
    @ToString
    private static class Demo3{
        //  类型推断
        public static void main(String[] args) {
            val list =new ArrayList<String>();
            var name="hello world";
            System.out.println(list);
            System.out.println(name);
        }
    }

    private static class Demo4{
        public void copyFile(String in,String out) throws IOException {
            // 自动关闭IO资源
            @Cleanup FileInputStream fis =new FileInputStream(in);
            @Cleanup FileOutputStream fos =new FileOutputStream(in);
            byte[] b=new byte[65536];
            while (true){
                int r= fis.read(b);
                if(r==-1){
                    break;
                }
                fos.write(b,0,r);
            }
        }
    }

    @ToString
    @Data
    @Accessors(fluent = true)
    private static class Demo5{
        //简化+链式
      private String name;
      private Integer age;

        public static void main(String[] args) {
            Demo5 demo5 = new Demo5();
            demo5.age(18).name("ubing");
            System.out.println(demo5);
        }

    }
    private static class Demo5_testRef{
        public static void main(String[] args) throws InstantiationException, IllegalAccessException {
            Class<Demo5> demo5Class = Demo5.class;
            Demo5 demo5 = demo5Class.newInstance();
            demo5.age(18);
            System.out.println(demo5);
            Field[] declaredFields = demo5Class.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                if(declaredField.getName()=="name"){
                    declaredField.set(demo5,"ubing");
                }else {
                    declaredField.set(demo5,18);
                }
            }
            System.out.println(demo5);
        }
    }
    @ToString
    @Data
    @Accessors(chain = true)
    private static class Demo6{
        // 链式
        private String name;
        private Integer age;

        public static void main(String[] args) {
            Demo6 demo6 = new Demo6();
            demo6.setAge(18).setName("ubing");
            System.out.println(demo6);
        }
    }

}
